package com.mxw.wechatai.service.impl;

import com.mxw.wechatai.service.WebCrawlerService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class WebCrawlerServiceImpl implements WebCrawlerService {

    @Override
    public String getWebContent(String url) throws IOException {
        // 设置超时时间并获取网页内容
        Document doc = Jsoup.connect(url)
                .timeout(10000)
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
                .get();

        // 移除不需要的元素
        doc.select("script, style, iframe, nav, footer, header, a").remove();

        // 获取主要内容
        StringBuilder content = new StringBuilder();
        
        // 获取标题
        String title = doc.title();
        if (title != null && !title.isEmpty()) {
            content.append("标题：").append(title).append("\n\n");
        }

        // 获取正文内容
        Elements paragraphs = doc.select("p, article, .content, .article, .post, .entry");
        for (Element p : paragraphs) {
            // 移除所有链接元素
            p.select("a").remove();
            String text = p.text().trim();
            if (!text.isEmpty()) {
                if (text.contains("下载地址")||text.contains("本文链接")){
                    continue;
                }
                content.append(text).append("\n\n");
            }
        }

        return content.toString().trim();
    }
} 